.SUFFIXES: .c .o .lo

COMPILE = $(CC) $(CFLAGS)
INC_PATH = -I../common -I../include
LIB_PATH = -L../client $(LIBS) -lfsclient -lfdirclient -lfastcommon -lserverframe
TARGET_LIB = $(TARGET_PREFIX)/$(LIB_VERSION)

API_SHARED_OBJS = fs_api.lo fs_api_allocator.lo fs_api_buffer_pool.lo \
                  write_combine/otid_htable.lo write_combine/obid_htable.lo \
                  write_combine/oid_htable.lo write_combine/timeout_handler.lo \
                  write_combine/combine_handler.lo \
                  read_ahead/otid_htable.lo read_ahead/obid_htable.lo

API_STATIC_OBJS = fs_api.o fs_api_allocator.o fs_api_buffer_pool.o \
                  write_combine/otid_htable.o write_combine/obid_htable.o \
                  write_combine/oid_htable.o write_combine/timeout_handler.o \
                  write_combine/combine_handler.o \
                  read_ahead/otid_htable.o read_ahead/obid_htable.o

HEADER_FILES = fs_api.h fs_api_allocator.h fs_api_buffer_pool.h fs_api_types.h

ALL_OBJS = $(API_STATIC_OBJS) $(API_SHARED_OBJS)

ALL_PRGS = write_combine/test_otid_htable
SHARED_LIBS = libfsapi.so
STATIC_LIBS = libfsapi.a
ALL_LIBS = $(SHARED_LIBS) $(STATIC_LIBS)

all: $(ALL_OBJS) $(ALL_PRGS) $(ALL_LIBS)
libfsapi.so: $(API_SHARED_OBJS)
	$(COMPILE) -o $@ -shared $(API_SHARED_OBJS) $(LIB_PATH)
libfsapi.a: $(API_STATIC_OBJS)
	ar rcs $@ $(API_STATIC_OBJS)
.o:
	$(COMPILE) -o $@ $<  $(API_STATIC_OBJS) $(LIB_PATH) $(INC_PATH)
.c:
	$(COMPILE) -o $@ $<  $(API_STATIC_OBJS) $(LIB_PATH) $(INC_PATH)
.c.o:
	$(COMPILE) -c -o $@ $<  $(INC_PATH)
.c.lo:
	$(COMPILE) -c -fPIC -o $@ $<  $(INC_PATH)
install:
	mkdir -p $(TARGET_LIB)
	mkdir -p $(TARGET_PREFIX)/lib
	mkdir -p $(TARGET_PREFIX)/include/fastsore/api
#mkdir -p $(TARGET_PREFIX)/include/fastsore/api/write_combine

	install -m 755 $(SHARED_LIBS) $(TARGET_LIB)
	install -m 644 $(HEADER_FILES) $(TARGET_PREFIX)/include/fastsore/api
	@BUILDROOT=$$(echo "$(TARGET_PREFIX)" | grep BUILDROOT); \
	if [ -z "$$BUILDROOT" ] && [ "$(TARGET_LIB)" != "$(TARGET_PREFIX)/lib" ]; then ln -sf $(TARGET_LIB)/libfsapi.so $(TARGET_PREFIX)/lib/libfsapi.so; fi
clean:
	rm -f $(ALL_OBJS) $(ALL_PRGS) $(ALL_LIBS)
